package org.eartha;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;

public class MapWriter extends MapActivity {
    
	private boolean isPotentialLongPress;
	private MapView mapView;
	private List<Overlay> mapOverlays;
	private HelloItemizedOverlay itemizedoverlay;
	private Drawable drawable;
	
	private boolean setItem;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
             
        mapOverlays = mapView.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.icon);
        
        itemizedoverlay = new HelloItemizedOverlay(drawable, this);
        GeoPoint point = new GeoPoint(19240000, -99120000);
        
        OverlayItem overlayitem = new OverlayItem(point,"Hola, Mundo!", "I'm in Mexico City!");
        
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
    	handleLongPress(event);
    	return super.dispatchTouchEvent(event);
    }
    
	private void handleLongPress(MotionEvent event) {
		
		try {
		if(event.getAction() == MotionEvent.ACTION_UP) {
			if(setItem) {
				setItem = false;
				Projection p = mapView.getProjection();
				GeoPoint geoPoint = p.fromPixels((int)event.getX(), (int) event.getY());
			
				OverlayItem newOverlayItem = new OverlayItem(geoPoint, "Hi, I'm a new Point", "wazaaap");
			
				itemizedoverlay.addOverlay(newOverlayItem);
			}
		} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
			
		}
		
		} catch(Error er) {
			er.printStackTrace(); //Error handling here
		}
		
	}
	
	private boolean isLongPressDetected() {
		isPotentialLongPress=true;
		try {
			for(int i = 0; i < 50; i++) {
				Thread.sleep(10);
				if(!isPotentialLongPress) {
					return false;
				}
			}
			return true;
		} catch (InterruptedException e) {
			return false;
		} finally {
			isPotentialLongPress = false;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch(item.getItemId()) {
		case R.id.new_forum:
			setItem = true; //creates new Forum by clicking
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}